package com.cat.model.biz.service.api.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.json.JSONArray;
import com.alonew.json.JSONException;
import com.alonew.json.JSONObject;
import com.cat.model.biz.service.api.InfoSearchService;
import com.cat.model.common.model.common.DressupInfoVO;
import com.cat.model.common.model.common.DressupScalePictureVO;
import com.cat.model.common.model.common.DressupStyleVO;
import com.cat.model.common.model.common.DressupZedCardVO;
import com.cat.model.common.model.common.GraphicsInfoVO;
import com.cat.model.common.model.common.GraphicsScalePictureVO;
import com.cat.model.common.model.common.GraphicsStyleVO;
import com.cat.model.common.model.common.GraphicsZedCardVO;
import com.cat.model.common.model.common.MakeupInfoVO;
import com.cat.model.common.model.common.MakeupScalePictureVO;
import com.cat.model.common.model.common.MakeupStyleVO;
import com.cat.model.common.model.common.MakeupZedCardVO;
import com.cat.model.common.model.common.PhotographerInfoVO;
import com.cat.model.common.model.common.PhotographerScalePictureVO;
import com.cat.model.common.model.common.PhotographerStyleVO;
import com.cat.model.common.model.common.PhotographerZedCardVO;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.common.util.ImageUtil;
import com.cat.model.common.util.PageUtil;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.bean.InfoSearchBean;
import com.cat.model.web.bean.ModelInfoSearchBean;

@Service("infoSearchService")
public class InfoSearchServiceImpl implements InfoSearchService{
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Override
	public String photographerList(ModelInfoSearchBean searchBean, int page, int size){
		List<InfoSearchBean> searchList = plmCommDAO.getPhotographerList(searchBean);
		
		List<InfoSearchBean> beanList = new ArrayList<InfoSearchBean>();
		JSONObject json = ErrorCode.OK.responseJSON();
		if(searchList == null || searchList.size() <= 0){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				return ErrorCode.SERVER_ERROR.response();
			}
			return json.toString();
		}
		
		for(InfoSearchBean vo : searchList){			
			InfoSearchBean bean = new InfoSearchBean();
			bean.setId(vo.getId());
			bean.setName(vo.getName());
			bean.setCityCode(vo.getCityCode());
			bean.setPrice(vo.getPrice());
			bean.setPhoneNo(vo.getPhoneNo());
			bean.setRichResume(vo.getRichResume());
			bean.setImage( ImageUtil.wrapImageUrl(vo.getImage()));
			
			beanList.add(bean);
		}
		
		int[] indexs = PageUtil.getStartAndEnd(page, size, beanList.size());
		 List<InfoSearchBean> resultList = beanList.subList(indexs[0], indexs[1]);
		
		try {
			json.put("data", new JSONArray(resultList));
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}
	
	@Override
	public String photographerInfo(long photographerId) {
		PhotographerInfoVO photographerInfo = modelMapper.getPhotographerInfo(photographerId);
		if(null == photographerInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			infoJson.put("name", photographerInfo.getName());
			infoJson.put("price", photographerInfo.getPrice());
			infoJson.put("cityCode", photographerInfo.getCityCode());
			infoJson.put("phoneNo", photographerInfo.getPhoneNo());
			infoJson.put("richResume", photographerInfo.getRichResume());
			infoJson.put("remark", photographerInfo.getRemark());
			infoJson.put("image", ImageUtil.wrapImageUrl(photographerInfo.getImage()));
			
			List<PhotographerStyleVO> styles = modelMapper.getPhotographerStyleList(photographerInfo.getId());
			JSONArray styleArray = new JSONArray();
			if(styles!=null&&styles.size()>0){
				for(PhotographerStyleVO style : styles){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("styleCode", style.getStyleCode());
					
					styleArray.put(detailJson);
				}
			}
			infoJson.put("styleList", styleArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String dressupList(ModelInfoSearchBean searchBean, int page, int size) {
		List<InfoSearchBean> searchList = plmCommDAO.getDressupList(searchBean);
		
		List<InfoSearchBean> beanList = new ArrayList<InfoSearchBean>();
		JSONObject json = ErrorCode.OK.responseJSON();
		if(searchList == null || searchList.size() <= 0){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				return ErrorCode.SERVER_ERROR.response();
			}
			return json.toString();
		}
		
		for(InfoSearchBean vo : searchList){			
			InfoSearchBean bean = new InfoSearchBean();
			bean.setId(vo.getId());
			bean.setName(vo.getName());
			bean.setCityCode(vo.getCityCode());
			bean.setPrice(vo.getPrice());
			bean.setPhoneNo(vo.getPhoneNo());
			bean.setRichResume(vo.getRichResume());
			bean.setImage( ImageUtil.wrapImageUrl(vo.getImage()));
			
			beanList.add(bean);
		}
		
		int[] indexs = PageUtil.getStartAndEnd(page, size, beanList.size());
		 List<InfoSearchBean> resultList = beanList.subList(indexs[0], indexs[1]);
		
		try {
			json.put("data", new JSONArray(resultList));
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String dressupInfo(long dressupId) {
		DressupInfoVO dressupInfo = modelMapper.getDressupInfo(dressupId);
		if(null == dressupInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			infoJson.put("name", dressupInfo.getName());
			infoJson.put("price", dressupInfo.getPrice());
			infoJson.put("cityCode", dressupInfo.getCityCode());
			infoJson.put("phoneNo", dressupInfo.getPhoneNo());
			infoJson.put("richResume", dressupInfo.getRichResume());
			infoJson.put("remark", dressupInfo.getRemark());
			infoJson.put("image", ImageUtil.wrapImageUrl(dressupInfo.getImage()));
			
			List<DressupStyleVO> styles = modelMapper.getDressupStyleList(dressupInfo.getId());
			JSONArray styleArray = new JSONArray();
			if(styles!=null&&styles.size()>0){
				for(DressupStyleVO style : styles){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("styleCode", style.getStyleCode());
					
					styleArray.put(detailJson);
				}
			}
			infoJson.put("styleList", styleArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String makeupList(ModelInfoSearchBean searchBean, int page, int size) {
		List<InfoSearchBean> searchList = plmCommDAO.getMakeupList(searchBean);
		
		List<InfoSearchBean> beanList = new ArrayList<InfoSearchBean>();
		JSONObject json = ErrorCode.OK.responseJSON();
		if(searchList == null || searchList.size() <= 0){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				return ErrorCode.SERVER_ERROR.response();
			}
			return json.toString();
		}
		
		for(InfoSearchBean vo : searchList){			
			InfoSearchBean bean = new InfoSearchBean();
			bean.setId(vo.getId());
			bean.setName(vo.getName());
			bean.setCityCode(vo.getCityCode());
			bean.setPrice(vo.getPrice());
			bean.setPhoneNo(vo.getPhoneNo());
			bean.setRichResume(vo.getRichResume());
			bean.setImage( ImageUtil.wrapImageUrl(vo.getImage()));
			
			beanList.add(bean);
		}
		
		int[] indexs = PageUtil.getStartAndEnd(page, size, beanList.size());
		 List<InfoSearchBean> resultList = beanList.subList(indexs[0], indexs[1]);
		
		try {
			json.put("data", new JSONArray(resultList));
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String makeupInfo(long makeupId) {
		MakeupInfoVO makeupInfo = modelMapper.getMakeupInfo(makeupId);
		if(null == makeupInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			infoJson.put("name", makeupInfo.getName());
			infoJson.put("price", makeupInfo.getPrice());
			infoJson.put("cityCode", makeupInfo.getCityCode());
			infoJson.put("phoneNo", makeupInfo.getPhoneNo());
			infoJson.put("richResume", makeupInfo.getRichResume());
			infoJson.put("remark", makeupInfo.getRemark());
			infoJson.put("image", ImageUtil.wrapImageUrl(makeupInfo.getImage()));
			
			List<MakeupStyleVO> styles = modelMapper.getMakeupStyleList(makeupInfo.getId());
			JSONArray styleArray = new JSONArray();
			if(styles!=null&&styles.size()>0){
				for(MakeupStyleVO style : styles){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("styleCode", style.getStyleCode());
					
					styleArray.put(detailJson);
				}
			}
			infoJson.put("styleList", styleArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String graphicsList(ModelInfoSearchBean searchBean, int page, int size) {
		List<InfoSearchBean> searchList = plmCommDAO.getGraphicsList(searchBean);
		
		List<InfoSearchBean> beanList = new ArrayList<InfoSearchBean>();
		JSONObject json = ErrorCode.OK.responseJSON();
		if(searchList == null || searchList.size() <= 0){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				return ErrorCode.SERVER_ERROR.response();
			}
			return json.toString();
		}
		
		for(InfoSearchBean vo : searchList){			
			InfoSearchBean bean = new InfoSearchBean();
			bean.setId(vo.getId());
			bean.setName(vo.getName());
			bean.setCityCode(vo.getCityCode());
			bean.setPrice(vo.getPrice());
			bean.setPhoneNo(vo.getPhoneNo());
			bean.setRichResume(vo.getRichResume());
			bean.setImage( ImageUtil.wrapImageUrl(vo.getImage()));
			
			beanList.add(bean);
		}
		
		int[] indexs = PageUtil.getStartAndEnd(page, size, beanList.size());
		 List<InfoSearchBean> resultList = beanList.subList(indexs[0], indexs[1]);
		
		try {
			json.put("data", new JSONArray(resultList));
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String graphicsInfo(long graphicsId) {
		GraphicsInfoVO graphicsInfo = modelMapper.getGraphicsInfo(graphicsId);
		if(null == graphicsInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			infoJson.put("name", graphicsInfo.getName());
			infoJson.put("price", graphicsInfo.getPrice());
			infoJson.put("cityCode", graphicsInfo.getCityCode());
			infoJson.put("phoneNo", graphicsInfo.getPhoneNo());
			infoJson.put("richResume", graphicsInfo.getRichResume());
			infoJson.put("remark", graphicsInfo.getRemark());
			infoJson.put("image", ImageUtil.wrapImageUrl(graphicsInfo.getImage()));
			
			List<GraphicsStyleVO> styles = modelMapper.getGraphicsStyleList(graphicsInfo.getId());
			JSONArray styleArray = new JSONArray();
			if(styles!=null&&styles.size()>0){
				for(GraphicsStyleVO style : styles){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("styleCode", style.getStyleCode());
					
					styleArray.put(detailJson);
				}
			}
			infoJson.put("styleList", styleArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String dressupScalePicture(long dressupId) {
		DressupInfoVO dressupInfo = modelMapper.getDressupInfo(dressupId);
		if(null == dressupInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			List<DressupScalePictureVO> pictures = modelMapper.getDressupScalePictureList(dressupInfo.getId());
			JSONArray pictureArray = new JSONArray();
			if(pictures!=null&&pictures.size()>0){
				for(DressupScalePictureVO picture : pictures){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("dressupId", picture.getDressupId());
					detailJson.put("mateType", picture.getMateType());
					detailJson.put("image", picture.getImage());
					
					pictureArray.put(detailJson);
				}
			}
			infoJson.put("pictureList", pictureArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String dressupZedCard(long dressupId) {
		DressupInfoVO dressupInfo = modelMapper.getDressupInfo(dressupId);
		if(null == dressupInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			List<DressupZedCardVO> zedCards = modelMapper.getDressupZedCardList(dressupInfo.getId());
			JSONArray zedCardArray = new JSONArray();
			if(zedCards!=null&&zedCards.size()>0){
				for(DressupZedCardVO zedCard : zedCards){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("dressupId", zedCard.getDressupId());
					detailJson.put("image", zedCard.getImage());
					
					zedCardArray.put(detailJson);
				}
			}
			infoJson.put("zedCardList", zedCardArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String photographerScalePicture(long photographerId) {
		PhotographerInfoVO photographerInfo = modelMapper.getPhotographerInfo(photographerId);
		if(null == photographerInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			List<PhotographerScalePictureVO> pictures = modelMapper.getPhotographerScalePictureList(photographerInfo.getId());
			JSONArray pictureArray = new JSONArray();
			if(pictures!=null&&pictures.size()>0){
				for(PhotographerScalePictureVO picture : pictures){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("photographerId", picture.getPhotographerId());
					detailJson.put("mateType", picture.getMateType());
					detailJson.put("image", picture.getImage());
					
					pictureArray.put(detailJson);
				}
			}
			infoJson.put("pictureList", pictureArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String photographerZedCard(long photographerId) {
		PhotographerInfoVO photographerInfo = modelMapper.getPhotographerInfo(photographerId);
		if(null == photographerInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			List<PhotographerZedCardVO> zedCards = modelMapper.getPhotographerZedCardList(photographerInfo.getId());
			JSONArray zedCardArray = new JSONArray();
			if(zedCards!=null&&zedCards.size()>0){
				for(PhotographerZedCardVO zedCard : zedCards){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("photographerId", zedCard.getPhotographerId());
					detailJson.put("image", zedCard.getImage());
					
					zedCardArray.put(detailJson);
				}
			}
			infoJson.put("zedCardList", zedCardArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String makeupScalePicture(long makeupId) {
		MakeupInfoVO makeupInfo = modelMapper.getMakeupInfo(makeupId);
		if(null == makeupInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			List<MakeupScalePictureVO> pictures = modelMapper.getMakeupScalePictureList(makeupInfo.getId());
			JSONArray pictureArray = new JSONArray();
			if(pictures!=null&&pictures.size()>0){
				for(MakeupScalePictureVO picture : pictures){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("makeupId", picture.getMakeupId());
					detailJson.put("mateType", picture.getMateType());
					detailJson.put("image", picture.getImage());
					
					pictureArray.put(detailJson);
				}
			}
			infoJson.put("pictureList", pictureArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String makeupZedCard(long makeupId) {
		MakeupInfoVO makeupInfo = modelMapper.getMakeupInfo(makeupId);
		if(null == makeupInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			List<MakeupZedCardVO> zedCards = modelMapper.getMakeupZedCardList(makeupInfo.getId());
			JSONArray zedCardArray = new JSONArray();
			if(zedCards!=null&&zedCards.size()>0){
				for(MakeupZedCardVO zedCard : zedCards){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("makeupId", zedCard.getMakeupId());
					detailJson.put("image", zedCard.getImage());
					
					zedCardArray.put(detailJson);
				}
			}
			infoJson.put("zedCardList", zedCardArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String graphicsScalePicture(long graphicsId) {
		GraphicsInfoVO graphicsInfo = modelMapper.getGraphicsInfo(graphicsId);
		if(null == graphicsInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			List<GraphicsScalePictureVO> pictures = modelMapper.getGraphicsScalePictureList(graphicsInfo.getId());
			JSONArray pictureArray = new JSONArray();
			if(pictures!=null&&pictures.size()>0){
				for(GraphicsScalePictureVO picture : pictures){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("graphicsId", picture.getGraphicsId());
					detailJson.put("mateType", picture.getMateType());
					detailJson.put("image", picture.getImage());
					
					pictureArray.put(detailJson);
				}
			}
			infoJson.put("pictureList", pictureArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String graphicsZedCard(long graphicsId) {
		GraphicsInfoVO graphicsInfo = modelMapper.getGraphicsInfo(graphicsId);
		if(null == graphicsInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			List<GraphicsZedCardVO> zedCards = modelMapper.getGraphicsZedCardList(graphicsInfo.getId());
			JSONArray zedCardArray = new JSONArray();
			if(zedCards!=null&&zedCards.size()>0){
				for(GraphicsZedCardVO zedCard : zedCards){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("graphicsId", zedCard.getGraphicsId());
					detailJson.put("image", zedCard.getImage());
					
					zedCardArray.put(detailJson);
				}
			}
			infoJson.put("zedCardList", zedCardArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}
	
}
